package com.yxhl.zoume.common.ui.custom;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.yxhl.zoume.R;
import com.yxhl.zoume.utils.CollectionUtils;
import com.yxhl.zoume.utils.LOG;

import java.io.Serializable;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import me.zhanghai.android.materialprogressbar.MaterialProgressBar;

/**
 * Created by vhawk on 2016/11/4.
 */

public abstract class PullToRefreshAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final String TAG = "UpPullLoadDataAda";
    private static final int CONTENT_VIEW_TYPE = 0;
    private static final int FOOT_VIEW_TYPE = 1;
    protected Context mContext;
    protected LinearLayoutManager mLayoutManager;
    protected List<? extends Serializable> mDataList;
    /**
     * RecyclerView 上拉加载图标是否可见
     */
    private FootViewHolder mFootViewHolder;

    public PullToRefreshAdapter(Context mContext,
                                LinearLayoutManager mLayoutManager) {
        this.mContext = mContext;
        this.mLayoutManager = mLayoutManager;
    }

    @Override
    public int getItemViewType(int position) {
        Log.v(TAG, "position = " + position);
        if (position < getCustomItemCount()) {
            return CONTENT_VIEW_TYPE;
        } else {
            return FOOT_VIEW_TYPE;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Log.v(TAG, "viewType = " + viewType);
        if (viewType == FOOT_VIEW_TYPE) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_recycler_passenger_foot, parent, false);
            return new FootViewHolder(view);
        } else {
            return onCustomCreateViewHolder(parent, viewType);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof FootViewHolder) {
            mFootViewHolder = (FootViewHolder) holder;
            Log.v(TAG, "holder instanceof FootViewHolder");
        } else {
            onCustomBindViewHolder(holder, position);
            Log.v(TAG, "holder not instanceof FootViewHolder");
        }
    }

    @Override
    public int getItemCount() {
        LOG.v(TAG, "getCustomItemCount = " + getCustomItemCount());
        return getCustomItemCount() + 1;
    }

    private void setFootViewVisibility(boolean isVisible) {
        if (mFootViewHolder != null) {
            mFootViewHolder.setVisible(isVisible);
        }
    }

    public void startRefreshing() {
        setFootViewVisibility(true);
    }

    public void setDataList(List<? extends Serializable> mDataList) {
        this.mDataList = mDataList;
        this.notifyDataSetChanged();
    }

    public void addDataList(List dataList) {
        if (CollectionUtils.isEmpty(mDataList)) {
            mDataList = dataList;
        } else {
            mDataList.addAll(dataList);
        }
        this.notifyDataSetChanged();
    }

    public abstract RecyclerView.ViewHolder onCustomCreateViewHolder(ViewGroup parent, int viewType);

    public abstract void onCustomBindViewHolder(RecyclerView.ViewHolder holder, int position);

    public abstract int getCustomItemCount();

    public void stopRefreshing() {
        setFootViewVisibility(false);
    }

    class FootViewHolder extends RecyclerView.ViewHolder {
        public static final int UP_PULL_REFRESH_DELAY = 600;
        @BindView(R.id.rl_up_pull_foot_container)
        RelativeLayout relativeLayout;

        @BindView(R.id.bar_up_pull_foot)
        MaterialProgressBar upPullFootBar;

        public FootViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(FootViewHolder.this, itemView);
        }

        void setVisible(boolean enableVisible) {
            if (enableVisible) {
                relativeLayout.setVisibility(View.VISIBLE);
                relativeLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Log.v(TAG, "pull");
                    }
                }, UP_PULL_REFRESH_DELAY);

            } else {
                relativeLayout.setVisibility(View.GONE);
            }
        }
    }

}
